import { _decorator, Collider2D, Color, Component, Node } from 'cc';
import { UnitSpacecraftBase } from './UnitSpacecraftBase';
import { Global, UnitType } from '../Util/Global';
import { Unit } from './Unit';
import { UnitPlanet } from './UnitPlanet';
import { Drawlayer, UICtrl } from '../Ctrl/UICtrl';
const { ccclass, property } = _decorator;

@ccclass('UnitAwacs')
export class UnitAwacs extends UnitSpacecraftBase {

    protected start(): void {
        super.start();
        this.unitType = UnitType.UnitAwacs;
    }

    protected onEnable(): void {
        super.onEnable();
    }

    protected onDisable(): void {
        super.onDisable();
        this._changeCount = 0;
    }

    private _changeMoveInterval: number = 3;
    private _changeCount: number = 0;

    protected updateLogic(dt: number) {
        let tooFar = this.TooFarFromParent();
        if (this.target && !this.target.death) {

            if (this.DistanceOther(this.target) > this.alertFieldRadius) {

                this.ChangeChaseTargetDir();
            }
            else{
                
                //当自己和目标相距太近时，环绕
                this.SurroundTargetDir();
            }
        }
        else if (!tooFar) {

            if (!this.target || this.target.death || this.target == this.parentUnit) { //从父级获取目标
                if (this.parentUnit) {

                    this.target = (<UnitPlanet>this.parentUnit).FindEnemyNearest() || this.parentUnit;;
                }
            }
            if (this.target == this) {
                console.log('警告==>目标是自己！！？');
            }

            if (this.target) {
                this.ChangeChaseTargetDir();
            }
        }

        //太远了不管有没有目标在下次tick时回家
        if (tooFar) {
            this.TimeChangeMoveDir(true);
        }

        if (this._changeCount == 0) {
            this.TimeChangeMoveDir();
        }

        this.AutoMove();

        if(this.parentUnit && this.parentUnit.playerCtrl){
            UICtrl.CircleRenderer(this.unitWorldPos, this.alertFieldRadius, Color.YELLOW, Drawlayer.Game);
        }
    }

    //延迟改变移动方向
    TimeChangeMoveDir(back?: boolean) {

        if (this._lifeTime > this._changeMoveInterval * this._changeCount) {
            this.ChangeMoveDir(back);
            this._changeCount++;
        }
    }
    AddTarget(t: Unit) {
        super.AddTarget(t);
        if (this.parentUnit)
            this.parentUnit.AddEnemy(t);
    }

    LostTarget(t: Unit) {
        super.LostTarget(t);
        if (this.parentUnit)
            this.parentUnit.LostEnemy(t);
    }

    protected onTriggerEnter(selfCollider: Collider2D, otherCollider: Collider2D) {
        super.onTriggerEnter(selfCollider, otherCollider);
        // 只在两个碰撞体开始接触时被调用一次
        let other = otherCollider.node.getComponent(Unit);

        if (other && other != this.parentUnit) {//对方不是自己父级
            if (other.unitType == UnitType.UnitPlanet || other instanceof UnitSpacecraftBase) {
                this.AddTarget(other);
                if(this.target == this.parentUnit){
                    this.target = other;
                }
                else{
                    this.target = this.FindTargetWeightAndNearest(other);
                }
            }
        }
    }

    protected onTriggerExit(selfCollider: Collider2D, otherCollider: Collider2D) {
        super.onTriggerExit(selfCollider, otherCollider);
        let other = otherCollider.node.getComponent(Unit);

        if (other && other != this.parentUnit) {
            if (other.unitType == UnitType.UnitPlanet || other instanceof UnitSpacecraftBase) {
                this.LostTarget(other);
            }
        }
    }

}


